# https://www.cnblogs.com/imyalost/p/7420924.html
import requests
import random
import json
import time
from MySqlClient import MysqlClient
from multiprocessing import Process
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

baseUrl = "https://t.cn/EGBa3wJ"
headers = {
    "referer": "http://te.tiaolll.gq/t.php?EbSeKAX.css",
    "User-Agent": "Mozilla/5.0 (Linux; U; Android 9; zh-cn; ONEPLUS A5000 Build/PKQ1.180716.001) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/8.9 Mobile Safari/537.36",
    # 自己用Fiddle抓包找
    # "sign": "7d056deeb002246a889f1e4a720e8608b1d70174",
    "ETag": "5c29c151-1ba8"
}
proxies = {
}
name = ''


# GET https://url.cn/5TijFbF HTTP/1.1
# Host: url.cn
# Connection: keep-alive
# User-Agent:
# Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,image/sharpp,image/apng,image/tpg,*/*;q=0.8
# Accept-Encoding: gzip, deflate
# Accept-Language: zh-CN,en-US;q=0.8
# HTTP/1.1 302 Found
# Server: nginx
# Date: Wed, 09 Jan 2019 02:28:32 GMT
# Content-Type: text/html
# Content-Length: 0
# Connection: keep-alive
# Location: http://tc.tiaolll.gq/t.php?EG1EMpf.js
def add_proxy():
    mysql = MysqlClient()
    proxies['http'] = mysql.random_proxy()


def go(count):
    add_proxy()
    options = webdriver.ChromeOptions()
    # options.add_argument('user-agent="Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Mobile Safari/537.36"')
    options.add_argument('--headless')
    options.add_argument('--disable-gpu')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-dev-shm-usage')
    mobile_emulation = {"deviceName": "Galaxy S5"}
    options.add_experimental_option("mobileEmulation", mobile_emulation)
    print(proxies)
    options.add_argument('--proxy-server={0}'.format(proxies['http']))
    browser = webdriver.Chrome(chrome_options=options)
    try:
        # url = "https://url.cn/54UGAwW"
        # url = "https://url.cn/5uwyxpr"#杨的
        url = baseUrl  # 夏
        browser.get(url)
        # print(browser.page_source)
        if '无法' in browser.page_source:
            print('代理不好使啦')
        else:
            time.sleep(10)
            try:
                btn = browser.find_element_by_id('goHome')
                if btn is not None:
                    print('clicked')
                    btn.click()
            except:
                print('source is ', browser.page_source)
            time.sleep(60)
            print(browser.page_source)
            print('success')
    except:
        print('error')
    finally:
        print('quit')
        browser.quit()

    return count


if __name__ == '__main__':
    while True:
        print('=== start ===')
        p1 = Process(target=go, args=(2,))
        p2 = Process(target=go, args=(2,))
        p3 = Process(target=go, args=(2,))
        p4 = Process(target=go, args=(2,))
        p5 = Process(target=go, args=(2,))
        p1.start()
        p2.start()
        p3.start()
        p4.start()
        p5.start()

        p1.join()
        p2.join()
        p3.join()
        p4.join()
        p5.join()
        print('end')
    # for i in range(0, 1):
    #     try:
    #         add_proxy()
    #         get = requests.get('https://url.cn/54UGAwW', proxies=proxies, headers=headers)
    #         requests.get('http://t.cn/EbH16MM', proxies=proxies, headers=headers)
    #
    #         get2 = requests.get('http://www.luoboapp.cf/?f=55215', proxies=proxies, headers=headers)
    #         print("success")
    #     except:
    #         print("失败")
